package com.example.admin.util;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;

public class DateUtils {
    public static void main(String[] args) {
        getLastMoth();
        getCurrMoth();
    }

    /**
     * 获取上月,只到月份 2024-06
     */
    public static void getLastMoth() {
        LocalDate today = LocalDate.now();
        YearMonth lastMonth = YearMonth.from(today.minusMonths(1));
        System.out.println(lastMonth);
        System.out.println(lastMonth.getMonthValue());
    }

    public static void getCurrMoth() {
        LocalDate now = LocalDate.now();
        // 获取本月第一天
        LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println("First day of month: " + firstDayOfMonth);
        // 获取本月最后一天
        LocalDate lastDayOfMonth = now.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("Last day of month: " + lastDayOfMonth);
    }

}
